package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.wx.mall.operate.dto.RebGoodsTwoDto;
import com.wx.mall.operate.dto.UpdateVGoodsDto;
import com.wx.mall.operate.dto.VGoodsDto;
import com.wx.mall.operate.entity.RebGoods;
import com.wx.mall.operate.entity.RebMiddle;
import com.wx.mall.operate.entity.VGoods;
import com.wx.mall.operate.mapper.VGoodsMapper;
import com.wx.mall.operate.service.IRebGoodsService;
import com.wx.mall.operate.service.IRebMiddleService;
import com.wx.mall.operate.service.IVGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author j
 * @since 2020-06-09
 */
@Service
public class VGoodsServiceImpl extends ServiceImpl<VGoodsMapper, VGoods> implements IVGoodsService {

    @Autowired
    IVGoodsService ivGoodsService;

    @Autowired
    IRebGoodsService iRebGoodsService;

    @Autowired
    IRebMiddleService iRebMiddleService;

    @Autowired
    RebMiddleServiceImpl rebMiddleService;

    @Override
    public int findGoodsA(Long ids) {
        QueryWrapper<VGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",ids);
        List<VGoods> list2=this.list(queryWrapper);
        int mab=0;
        for (VGoods v:list2
             ) {
            RebGoods rebGoods=new RebGoods();
            rebGoods.setId(v.getId());
            RebGoods byId = iRebGoodsService.getById(rebGoods.getId());
            byId.setStatus(1);
            boolean b = iRebGoodsService.updateById(byId);
            if (b==true){
                mab=1;
            }else {
                mab=0;
            }
        }
        return mab;
    }

    @Override
    public List<VGoods> findGoodsAll(VGoodsDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<VGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",dto.getIds());
        List<VGoods> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<VGoods> findScreenVGoods(RebGoodsTwoDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<VGoods> queryWrapper=new QueryWrapper<>();
        if (dto.getSortId()!=null){
            queryWrapper.eq("sort_id",dto.getSortId()).eq("rebate_id",dto.getIds());
        }else if (dto.getBrandId()!=null){
            queryWrapper.eq("brand_id",dto.getBrandId()).eq("rebate_id",dto.getIds());
        }else if (dto.getTitle()!=null){
            queryWrapper.like("title",dto.getTitle()).eq("rebate_id",dto.getIds());
        }
        List<VGoods> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public int findVUpdate(Long ids,List<VGoods> vGoods) {
        QueryWrapper<VGoods> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("rebate_id",ids);
        List<VGoods> list = this.list(queryWrapper);
        int mm=1;
        if (list.size()==0){
            for (VGoods v : vGoods
            ) {
                RebMiddle middle=new RebMiddle();
                middle.setBrandId(v.getBrandId());
                middle.setGoodsId(v.getId());
                middle.setRebateId(v.getRebateId());
                middle.setSortId(v.getSortId());
                boolean save = iRebMiddleService.save(middle);
                if (save==true) {
                    RebGoods byId = iRebGoodsService.getById(v.getId());
                    byId.setStatus(1);
                    boolean b = iRebGoodsService.updateById(byId);
                    if (b == true) {
                        mm = 1;
                    } else {
                        mm = 0;
                    }
                }
            }
        }else {
            int i = rebMiddleService.deleteMid(ids);
            if (i==1){
                for (VGoods b : vGoods
                        ) {
                    RebMiddle middles=new RebMiddle();
                    middles.setBrandId(b.getBrandId());
                    middles.setGoodsId(b.getId());
                    middles.setRebateId(b.getRebateId());
                    middles.setSortId(b.getSortId());
                    boolean save = iRebMiddleService.save(middles);
                    if (save==true) {
                        RebGoods byId = iRebGoodsService.getById(b.getId());
                        byId.setStatus(1);
                        boolean bs = iRebGoodsService.updateById(byId);
                        if (bs == true) {
                            mm = 1;
                        } else {
                            mm = 0;
                        }
                    }
                }
            }
        }
        return mm;
    }

}
